<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Hoodoo::Communicators::Pool::QueueWithTimeout</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="../../../../css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../../../css/main.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../../../css/github.css" type="text/css" media="screen" />
<script src="../../../../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../../js/main.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>
    <div class="banner">
        <h1>
            <span class="type">Class</span>
            Hoodoo::Communicators::Pool::QueueWithTimeout
                <span class="parent">&lt;
                    Object
                </span>
        </h1>
        <ul class="files">
            <li><a href="../../../../files/lib/hoodoo/communicators/pool_rb.html">lib/hoodoo/communicators/pool.rb</a></li>
        </ul>
    </div>
    <div id="bodyContent">
        <div id="content">
    <div class="description">
      
<p>Internal implementation detail of <a
href="../Pool.html">Hoodoo::Communicators::Pool</a>.</p>

<p>Since pool clients can say “wait until (one or all) workers have processed
their Queue contents”, we need to have some way of seeing when all work is
done. The clean way to do it is to push &#39;sync now&#39; messages onto
the communicator Threads work Queues, so that as they work through the
Queue they&#39;ll eventually reach that message. They then push a message
onto a sync Queue for that worker. Meanwhile the waiting pool does (e.g.) a
<code>pop</code> on the sync Queue, which means it blocks until the workers
say they&#39;ve finished. No busy waiting, Ruby gets to make its best guess
at scheduling, etc.; all good.</p>

<p>The catch? You can&#39;t use <code>Timeout::timeout...do...</code> around a
Queue <code>pop</code>. It just doesn&#39;t work. It&#39;s a strange
omission and requires code gymnastics to work around.</p>

<p>Enter <a href="QueueWithTimeout.html">QueueWithTimeout</a>, from:</p>

<pre><code>http://spin.atomicobject.com/2014/07/07/ruby-queue-pop-timeout/</code></pre>

    </div>








    <!-- Method ref -->
    <div class="sectiontitle">Methods</div>
    <dl class="methods">
        <dt>#</dt>
        <dd>
          <ul>
              <li>
                <a href="#method-i-3C-3C">&lt;&lt;</a>
              </li>
          </ul>
        </dd>
        <dt>N</dt>
        <dd>
          <ul>
              <li>
                <a href="#method-c-new">new</a>
              </li>
          </ul>
        </dd>
        <dt>S</dt>
        <dd>
          <ul>
              <li>
                <a href="#method-i-shift">shift</a>
              </li>
          </ul>
        </dd>
    </dl>










    <!-- Methods -->
      <div class="sectiontitle">Class Public methods</div>
        <div class="method">
          <div class="title method-title" id="method-c-new">
              <b>new</b>()
            <a href="../../../../classes/Hoodoo/Communicators/Pool/QueueWithTimeout.html#method-c-new" name="method-c-new" class="permalink">Link</a>
          </div>

            <div class="description">
              <p>Create a new instance.</p>
            </div>



            <div class="sourcecode">
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-c-new_source')" id="l_method-c-new_source">show</a>
              </p>
              <div id="method-c-new_source" class="dyn-source">
                <pre><span class="ruby-comment"># File lib/hoodoo/communicators/pool.rb, line 494</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">initialize</span>
  <span class="ruby-ivar">@mutex</span>    = <span class="ruby-operator">::</span><span class="ruby-constant">Mutex</span>.<span class="ruby-identifier">new</span>
  <span class="ruby-ivar">@queue</span>    = []
  <span class="ruby-ivar">@recieved</span> = <span class="ruby-operator">::</span><span class="ruby-constant">ConditionVariable</span>.<span class="ruby-identifier">new</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
          </div>
      <div class="sectiontitle">Instance Public methods</div>
        <div class="method">
          <div class="title method-title" id="method-i-3C-3C">
              <b>&lt;&lt;</b>( entry )
            <a href="../../../../classes/Hoodoo/Communicators/Pool/QueueWithTimeout.html#method-i-3C-3C" name="method-i-3C-3C" class="permalink">Link</a>
          </div>

            <div class="description">
              <p>Push a new entry to the end of the queue.</p>
<dl class="rdoc-list note-list"><dt>entry
<dd>
<p>Entry to put onto the end of the queue.</p>
</dd></dl>
            </div>



            <div class="sourcecode">
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-i-3C-3C_source')" id="l_method-i-3C-3C_source">show</a>
              </p>
              <div id="method-i-3C-3C_source" class="dyn-source">
                <pre><span class="ruby-comment"># File lib/hoodoo/communicators/pool.rb, line 504</span>
<span class="ruby-keyword">def</span> <span class="ruby-operator">&lt;&lt;</span>( <span class="ruby-keyword ruby-title">entry</span> )
  <span class="ruby-ivar">@mutex</span>.<span class="ruby-identifier">synchronize</span> <span class="ruby-keyword">do</span>
    <span class="ruby-ivar">@queue</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">entry</span>
    <span class="ruby-ivar">@recieved</span>.<span class="ruby-identifier">signal</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
          </div>
        <div class="method">
          <div class="title method-title" id="method-i-shift">
              <b>shift</b>( timeout = nil )
            <a href="../../../../classes/Hoodoo/Communicators/Pool/QueueWithTimeout.html#method-i-shift" name="method-i-shift" class="permalink">Link</a>
          </div>

            <div class="description">
              <p>Take an entry from the front of the queue (FIFO) with optional timeout if
the queue is empty.</p>
<dl class="rdoc-list note-list"><dt>timeout
<dd>
<p>Timeout (in seconds, Integer or Float) to wait for an item to appear on the
queue, if the queue is empty. If <code>nil</code>, there is no timeout
(waits indefinitely). Optional; default is <code>nil</code>.</p>
</dd></dl>

<p>If given a non-<code>nil</code> timeout value and the timeout expires,
raises a ThreadError exception (just as non-blocking Ruby Queue#pop would).</p>
            </div>



            <div class="sourcecode">
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-i-shift_source')" id="l_method-i-shift_source">show</a>
              </p>
              <div id="method-i-shift_source" class="dyn-source">
                <pre><span class="ruby-comment"># File lib/hoodoo/communicators/pool.rb, line 522</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">shift</span>( <span class="ruby-identifier">timeout</span> = <span class="ruby-keyword">nil</span> )
  <span class="ruby-ivar">@mutex</span>.<span class="ruby-identifier">synchronize</span> <span class="ruby-keyword">do</span>
    <span class="ruby-keyword">if</span> <span class="ruby-ivar">@queue</span>.<span class="ruby-identifier">empty?</span>
      <span class="ruby-ivar">@recieved</span>.<span class="ruby-identifier">wait</span>( <span class="ruby-ivar">@mutex</span>, <span class="ruby-identifier">timeout</span> ) <span class="ruby-keyword">if</span> <span class="ruby-identifier">timeout</span> <span class="ruby-operator">!=</span> <span class="ruby-number">0</span>
      <span class="ruby-identifier">raise</span>( <span class="ruby-constant">ThreadError</span>, <span class="ruby-string">&#39;queue empty&#39;</span> ) <span class="ruby-keyword">if</span> <span class="ruby-ivar">@queue</span>.<span class="ruby-identifier">empty?</span>
    <span class="ruby-keyword">end</span>

    <span class="ruby-ivar">@queue</span>.<span class="ruby-identifier">shift</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
          </div>
</div>

    </div>
  </body>
</html>
